package com.ecube.test;

import com.alibaba.fastjson.JSON;
import com.ecube.mqtt.MqttUtils;
import com.ecube.protocol.ProtocolHelper;
import com.ecube.protocol.charge.ChargeChargeState;
import com.ecube.protocol.charge.ChargeChargerState;
import com.ecube.protocol.common.DataReportHead;
import com.ecube.protocol.common.DataReportStandard;
import com.ecube.protocol.enums.ChargeFunctionEnum;
import com.ecube.utils.DateUtils;
import com.ecube.utils.EnumUtils;
import lombok.Builder;
import lombok.Data;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 充电站
 *  充放电状态
 *
 * @author zhuweijun
 * @version 1.0.0
 * @since 2025/8/11 14:04
 */
public class SendChargeChargeState {

    public static void main(String[] args) throws Exception {

        while (true){
            sendChargeState("FAT", "100110001", "1001100012025155",
                    ChargerInfo.builder()
                            .code("1001100012025155")
                            .orderSn("25155" + DateUtils.getNowDateMs())
                            .gunSize(2).build());

            sendChargeState("FAT", "100110001", "1001100012025152",
                    ChargerInfo.builder()
                            .code("1001100012025152")
                            .orderSn("25152" + DateUtils.getNowDateMs())
                            .gunSize(2).build());

            Thread.sleep(1000 * 10);
        }
    }

    private static void sendChargeState(String env, String productCode, String deviceCode, ChargerInfo info) {
        ChargeFunctionEnum function = (ChargeFunctionEnum) EnumUtils.getFunction(ChargeChargeState.class);
        if(function == null){
            return;
        }
        String pubTopic = productCode + "/" + deviceCode + "/" + function.getDirection() + "/" + function.getType();

        DataReportStandard reportStandard = new DataReportStandard();
        // 设置报文头
        DataReportHead reportHand = ProtocolHelper.getReportHand(productCode, deviceCode, function.getFunction());
        reportHand.setVersion("V2.0.0");
        reportStandard.setHeader(reportHand);

        ChargeChargeState chargeState = new ChargeChargeState();
        chargeState.setChgCode(info.getCode());
        chargeState.setGunCodeArray(Arrays.asList(1, 2));
        chargeState.setOrderSn(info.getOrderSn());

        // 充电接口列表
        List<ChargeChargeState.GunInfo> gunList = new ArrayList<>();
        for (Integer i = 1; i <= info.getGunSize(); i++) {
            ChargeChargeState.GunInfo gunInfo = new ChargeChargeState.GunInfo();
            gunInfo.setCode(i);
            gunList.add(gunInfo);
        }
        chargeState.setGunList(gunList);

        reportStandard.setDataBody(chargeState);
        String reportContent = JSON.toJSONString(reportStandard);
        MqttUtils.sendMQTT(env, pubTopic, reportContent);
    }

    /**
     * 充放电设备
     */
    @Data
    @Builder
    private static class ChargerInfo {

        /**
         * 设备编号
         */
        private String code;

        /**
         * 充电枪数量
         */
        private Integer gunSize;

        /**
         * 流水号
         */
        private String orderSn;
    }
}
